/**  
 * All rights Reserved, Designed By www.beonelot.com
 * @Package: com.bccah.modlib.dbdo.service.impl
 * @Filename: AcsControlBlockInfoServiceImpl.java
 * @Description: TODO(AcsControlBlockInfoService接口实现)
 * @author: 金鑫智慧
 * @date: 2020-04-03
 * @version: V1.0
 * @Copyright: 2018 www.beonelot.com Inc. All rights reserved.
 * 注意：本内容仅限于重庆金鑫智慧科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
package com.jinxin.platform.cdacsrecord.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinxin.platform.cdacsrecord.core.mapper.JurenAcsCommunityInfoMapper;
import com.jinxin.platform.cdacsrecord.core.mapper.JurenAcsControlBlockInfoMapper;
import com.jinxin.platform.cdacsrecord.core.mapper.JurenAcsControlDeviceInfoDao;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.AcsControlBlockInfoDO;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.AcsControlDeviceInfo;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.AcsControlUserInfoDO;
import com.jinxin.platform.cdacsrecord.core.pojo.domains.JurenAcsCommunityInfo;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.CommonCodeEntity;
import com.jinxin.platform.cdacsrecord.core.pojo.entity.Constant;
import com.jinxin.platform.cdacsrecord.core.pojo.vo.AcsBlockStatInfoVO;
import com.jinxin.platform.cdacsrecord.core.pojo.vo.ResultVO2;
import com.jinxin.platform.cdacsrecord.core.service.AcsControlBlockInfoService;
import com.jinxin.platform.cdacsrecord.core.service.RestTemplateService;
import com.jinxin.platform.cdacsrecord.core.utils.POIUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO(AcsControlBlockInfoService接口实现)
 * @Classn: AcsControlBlockInfoServiceImpl
 * @author SY
 * @date 2020-04-03
 */
@Service
@Slf4j
public class AcsControlBlockInfoServiceImpl implements AcsControlBlockInfoService {

    @Autowired
    private JurenAcsCommunityInfoMapper communityInfoMapper;
    @Autowired
    private JurenAcsControlBlockInfoMapper blockInfoMapper;
    @Autowired
    private RestTemplateService restTemplateService;

    @Override
    public AcsControlBlockInfoDO getById(String blockId) {
        return blockInfoMapper.selectByPrimaryKey(blockId);
    }

    @Override
    public List<Map> selAllBlock(){
        return  blockInfoMapper.selAllBlock();
    }

    @Override
    public List<Map> selAllBlockByState() {
        return blockInfoMapper.selAllBlockByState();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map uploadBlock(MultipartFile file, String community, String housingEstateId)throws Exception {
        Map<String,String > resMap = new HashMap<>();
        List<String[]> products = POIUtil.readExcel(file, 6);
        for (String[] prod : products){
            String[] productInfo = prod;
            if(StringUtils.isEmpty(productInfo[1])){
                continue;
            }
            AcsControlBlockInfoDO blockInfoDO = new AcsControlBlockInfoDO();
            blockInfoDO.setCompanyId(community);
            blockInfoDO.setBlockNo(productInfo[0]);
            blockInfoDO.setBlockName(productInfo[1]);
            int blockType = "楼栋".equals(productInfo[5]) ? 2 : 1; // 2 楼栋 1出入口
            blockInfoDO.setBlockType(blockType);
            blockInfoDO.setHousingEstateId(housingEstateId);
            ResultVO2 resultVO = restTemplateService.buildingblockOperate(Constant.OPERATION_SIGN_ADD, blockInfoDO);
            blockInfoDO.setBlockDesc(productInfo[2]);
            blockInfoDO.setBlockAddress(productInfo[3]);
            blockInfoDO.setBlockFloor(Integer.parseInt(productInfo[4]));
            blockInfoDO.setCreateTime(LocalDateTime.now());
            blockInfoDO.setState("0");
            if(resultVO != null && resultVO.getRstId().equals(Constant.ETO_TOKEN_RSTID_SUCCESS)){
                blockInfoDO.setBlockId(resultVO.getObject().toString());
                blockInfoMapper.insert(blockInfoDO);
            }
        }
        resMap.put("resultCode", CommonCodeEntity.SUC_CODE);
        resMap.put("resultDesc", "suc");
        return resMap;
    }

    @Override
    public PageInfo<AcsControlBlockInfoDO> selParamPage(int page, int size,String blockName,String community) {
        PageHelper.startPage(page,size);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<AcsControlBlockInfoDO> list = blockInfoMapper.selAllTable(blockName,community);
        PageInfo<AcsControlBlockInfoDO> pageInfo = new PageInfo<AcsControlBlockInfoDO>(list);
        return pageInfo;
    }

    @Override
    public int saveDO(AcsControlBlockInfoDO paramDO) {
        return blockInfoMapper.insert(paramDO);
    }

    @Override
    public int updateDO(AcsControlBlockInfoDO paramDO) {
        return blockInfoMapper.updateByPrimaryKeySelective(paramDO);
    }

    @Override
    public int delDO(String pid) {
        // 如果楼栋绑定设备，则删除失败
//        AcsControlBlockInfoDO blockInfoDO = blockInfoMapper.selectByPrimaryKey(pid);
//        if(null == blockInfoDO ){
//            log.error("未查询到楼栋信息--"+pid);
//            return 0;
//        }
//        AcsControlDeviceInfo acsControlDeviceInfo1 = deviceInfoDao.selectByBuildingLoc(blockInfoDO.getBlockName());
//        if(null == acsControlDeviceInfo1 ){
//            return blockInfoMapper.deleteByPrimaryKey(pid);
//        }else {
//            log.error(blockInfoDO.getBlockName()+"楼栋有绑定设备");
//            return 0;
//        }
        return blockInfoMapper.deleteByPrimaryKey(pid);
    }

    @Override
    public List<AcsControlBlockInfoDO> selDOByInfo(AcsControlBlockInfoDO paramDO) {
        return blockInfoMapper.selDOByInfo(paramDO );
    }

    @Override
    public List<AcsBlockStatInfoVO> listStatBlcokInfo(String companyId) {
        return blockInfoMapper.listStatBlcokInfo(companyId);
    }

    @Override
    public List getBlockInfoList(String blockId) {
//        String[] strs = blockNo.split("-");
//        HashMap param = new HashMap();
//        param.put("name", strs[0]);
//        JurenAcsCommunityInfo communityInfo = communityInfoMapper.selectOne(param);
//        if(communityInfo == null){
//            return null;
//        }
//        String companyId = communityInfo.getCompanyId();
//        HashMap param2 = new HashMap();
//        param2.put("blockName",strs[1]);
//        AcsControlBlockInfoDO blockInfoDO = blockInfoMapper.selectOne(param2);
//        if(blockInfoDO == null){
//            return null;
//        }
//        String blockId = blockInfoDO.getBlockId();
        if(StringUtils.isEmpty(blockId)){return null;}
        String[] blockIdArr = blockId.split(",");
        return blockInfoMapper.getBlockInfoList(blockIdArr);
    }
}
